/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hciproject;

/**
 *
 * @author Alerk
 */
public class BusinessFunction {
    private static final float THRESHOLD_1 = 100;
    private static final float THRESHOLD_2 = 100;
    private static final float THRESHOLD_3 = 10;

    private static final int NO_ACTION = 0;//no action
    private static final int ACTION_1 = 1;//backtap
    private static final int ACTION_2 = 1;//fronttap
    private static final int ACTION_3 = 1;//backswing
    private static final int ACTION_4 = 1;//frontswing

    /**
     * 1: compare the var of roll
     *  1.1 Var(roll) large: tapping => compare the Var((roll+pitch)/2)
     *      1.1.1 Var() small: backtap
     *      1.1.2 Var() large: fronttap
     *  1.2 Var(roll) small: swing => compare the Var((pitch+yaw)/2)
     *      1.2.1 Var() small: front swing
     *      1.2.2 Var() large: back swing
     * 2: TODO
     * store the data in 50 samples
     * calculate the Var     
     */
    public static int recognize(float[]roll, float[]pitch, float[]yaw, int length)
    {
        int action = NO_ACTION;
        if(BusinessFunction.variance(roll )>BusinessFunction.THRESHOLD_1)
        {
            //tapping
            float[]roll_pitch = BusinessFunction.average(roll, pitch);
            if(BusinessFunction.variance(roll_pitch )<BusinessFunction.THRESHOLD_2)
            {
                action = BusinessFunction.ACTION_1;
                System.out.println("Action 1: back tap");
            }
            else
            {
                action = BusinessFunction.ACTION_2;
                System.out.println("Action 2: front tap");
            }
        }
        else if(BusinessFunction.variance(roll )<BusinessFunction.THRESHOLD_3
                && BusinessFunction.variance(pitch )<BusinessFunction.THRESHOLD_3
                && BusinessFunction.variance(yaw )<BusinessFunction.THRESHOLD_3)
        {
            //NO ACTION

        }
        else
        {
            //swing
            float[]pitch_yaw = BusinessFunction.average(pitch, yaw);
            if(BusinessFunction.variance(pitch_yaw )<BusinessFunction.THRESHOLD_2)
            {
                action = BusinessFunction.ACTION_4;
                System.out.println("Action 4: front swing");
            }
            else
            {
                action = BusinessFunction.ACTION_3;
                System.out.println("Action 3: back swing");
            }
        }
        return action;
    }

    public static void testValue(float[]roll, float[]pitch, float[]yaw, int length)
    {
        System.out.println("E(roll) = " +BusinessFunction.expectedValue(roll)+
                " Var(roll) = "+BusinessFunction.variance(roll)+
                " Var((roll+pitch)/2) = "+BusinessFunction.variance(average(roll,pitch))+
                " Var((pitch+yaw)/2) = "+BusinessFunction.variance(average(yaw,pitch)));
    }

    public static float[] average(float[]a, float[]b)
    {
        int length = a.length;
        float[]result = new float[length];
        for(int i=0; i<length; i++)
        {
            result[i]=(a[i]+b[i])/2;
        }
        return result;
    }


    public static float expectedValue(float[]data)
    {
        int length = data.length;
        float result=0;
        for(int i=0; i<length; i++)
        {
            result+=data[i];
        }        
        return result/length;
    }
    public static float variance(float[]data)
    {
        float var = 0;
        int length = data.length;
        float expected = BusinessFunction.expectedValue(data );
        for(int i=0; i<length; i++)
        {
            var+=(data[i]-expected)*(data[i]-expected);
        }
        return var/length;
    }


}
